// src/main.js
import { createApp } from 'vue'
import './style.css'
import App from './App.vue'
import router from './router'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import store from './store'
import panelHead from './components/panelHead.vue'
import { lo } from 'element-plus/es/locales.mjs'

//刷新后动态路由添加
const localData = localStorage.getItem('admin')


if(localData)
{
  store.commit('dynamicMenu',JSON.parse(localData).menu.routerList)
  store.state.menu.routerList.forEach(item => {
    //动态路由添加
    router.addRoute('main', item)
  })
}

// 导入所有图标
import * as ElementPlusIconsVue from '@element-plus/icons-vue'

router.beforeEach((to, next) => {
  const token = window.localStorage.getItem('token')
  // 如果访问的不是登录页且没有token，则重定向到登录页
  if (to.path !== '/login' && !token) {
    return '/login'
  } else if (to.path === '/login' && token) {
    // 如果访问登录页且有token，则重定向到首页
    return '/'
  } else {
    return true
  }
})


const app = createApp(App)

// 注册所有图标组件
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component)
}
app.component('panelHead', panelHead)
app.use(router)
app.use(store)
app.use(ElementPlus)
app.mount('#app')